import subprocess
import os
from os import listdir

with open("datatoolsobj/__init__.py") as ini_f:
    global ver_main,ver_sub,ver_mod,data
    data = []
    for line in ini_f:
        data.append(line)
        line = line.strip()
        if line.find("__version__ =") != -1:
            _,version = line.split("=")
            print(version)
            version = version.replace('"','')
            ver = version.split(".")
            ver_main,ver_sub,ver_mod = ver
            ver_main = int(ver_main)
            ver_sub = int(ver_sub)
            print("识别：主版本号{}，子版本号{}，修改版本号{}".format(ver_main,ver_sub,ver_mod))
            ver_mod = int(ver_mod) + 1
            new_line = '__version__ = "{}.{}.{}"\r\n'.format(ver_main,ver_sub,ver_mod)
            print("新版本号:{}".format(new_line))
            data[-1] = new_line

with open("datatoolsobj/__init__.py","w+") as ini_w:
    ini_w.writelines(data)

with open("setup.py",encoding='utf-8',errors='ignore') as set_f:
    global set_data
    set_data = []
    for line in set_f:
        set_data.append(line)
        if line.find("version") != -1:
            _,version = line.split("=")
            print("set file:")
            print(version)
            new_line = '    version="{}.{}.{}",\n'.format(ver_main,ver_sub,ver_mod)
            print("包配置文件中新版本号:{}".format(new_line))
            set_data[-1] = new_line

with open("setup.py","w+") as set_w:
    set_w.writelines(set_data)

del_path = ".\\dist\\"
for file in listdir(del_path):
    if file.endswith(".whl"):
        os.remove(del_path+file)

proc = subprocess.run(["python","setup.py","bdist_wheel"])